<template>
	<view>
		<mvBar :mysNavConfig="mysNavConfig" @onLeftClick="onLeftClick"></mvBar>
		<view class="u-page">
			<u-toast ref="uToast"></u-toast>
			<u-form labelPosition="left">
				<u-form-item borderBottom>
					<u--input border="none" prefixIcon="lock" prefixIconStyle="font-size: 22px;color: #909399"
						v-model="newpassword" type="password" clearable placeholder="请输入新密码">
					</u--input>
				</u-form-item>
				<u-form-item borderBottom>
					<u--input border="none" prefixIcon="lock-fill" prefixIconStyle="font-size: 22px;color: #909399"
						v-model="againpassword" type="password" clearable placeholder="请再一次输入新密码">
					</u--input>
				</u-form-item>
			</u-form>
			<view style="width: 90%; margin:10% auto;">
				<u-button shape='circle' type="primary" text="确定" @click="alter">
				</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userinfo: '',
				mysNavConfig: {
					/* 占位开启  */
					navPadding: true,
					/* 背景 */
					bgColor: "#2979FF",
					/* 标题 （屏幕中心居中 两边图标中心居中使用slot center1） */
					navTitle: {
						text: "修改密码",
						color: "#ffffff",
						fontSize: "18px", // px upx rpx
						fontWeight: "500", // 100 - 700
					},
					isCustomFn: true,
					btnType: "type2",
					type2Config: {
						// 左图标
						leftPath: "/static/img/back.png",
						// 右图标
						rightPath: "/static/img/home.png",
						// 圆角
						radius: "40rpx",
					}
				},
				//新密码
				newpassword: '',
				// 再一次输入新密码
				againpassword: '',
			}
		},
		onLoad() {
			this.userinfo = uni.getStorageSync('userinfo');
		},
		methods: {
			//返回
			onLeftClick() {
				uni.switchTab({
					url: '/pages/tabbar/userCenter'
				});
			},
			//修改密码
			alter() {
				if (!this.newpassword || !this.againpassword) {
					this.$utils.showToast(this, '请输入密码！', 'warning');
					return;
				}
				if (this.newpassword.length < 6 || this.againpassword.length < 6) {
					this.$utils.showToast(this, '密码最少6位！', 'warning');
					return;
				}
				if (this.newpassword !== this.againpassword) {
					this.$utils.showToast(this, '两次输入密码不一致！', 'warning');
					return;
				}
				let pwd = new Object();
				pwd.Password = this.newpassword;
				pwd.RepeatPassword = this.againpassword;
				uni.showLoading({
					title: "修改中...",
					mask: true,
				});
				uni.request({
					url: getApp().globalData.serverAddress + this.$uri.Uri_changepassword.replace(
						"{userRealId}", this.userinfo.userRealId),
					data: JSON.stringify(pwd),
					dataType: "json",
					method: "POST",
					success: (resData) => {
						if (resData.statusCode !== 200) {
							this.$utils.showToast(this, '服务器错误,请联系技术支持人员!', 'error');
							return
						}
						if (resData.data.code == 0) {
							this.$utils.showToast(this, '修改成功！', 'success', 3,
								'/pages/tabbar/userCenter');
						} else {
							this.$utils.showToast(this, resData.data.msg, 'error');
						}
					},
					fail: () => {
						this.$utils.showToast(this, '修改失败！', 'error');
					},
					complete: () => {
						uni.hideLoading();
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.u-form-item__body {
		padding: 15px 0 !important;
	}

	.u-form {
		width: 95%;
		margin: auto;
	}
</style>